export default class Utils {
  constructor(options) {
    // const {  } = options;
  }
  //保留两位小数
  returnFloat(value){
    var value=Math.round(parseFloat(value)*100)/100;
    var xsd=value.toString().split(".");
    if(xsd.length==1){
    value=value.toString()+".00";
    return value;
    }
    if(xsd.length>1){
      if(xsd[1].length<2){
      value=value.toString()+"0";
      }
      return value;
    }
  }
  //获取从今天开始计算的日期
  getPreDay(day) {
    let today = new Date();
    let targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
    today.setTime(targetday_milliseconds);
    let tYear = today.getFullYear();
    let tMonth = today.getMonth();
    let tDate = today.getDate();
    tMonth = this.doHandleMonth(tMonth + 1);
    tDate = this.doHandleMonth(tDate);
    return tYear + "-" + tMonth + "-" + tDate + " " + "23:59:59";
  }
  doHandleMonth(month) {
    let m = month;
    if (month.toString().length == 1) {
        m = "0" + month;
    }
    return m;
  }

  //加法函数
  addition(a, b) {
    var c, d, e;
    try {
        c = a.toString().split(".")[1].length;
    } catch (f) {
        c = 0;
    }
    try {
        d = b.toString().split(".")[1].length;
    } catch (f) {
        d = 0;
    }
    return e = Math.pow(10, Math.max(c, d)), (multiplication(a, e) + multiplication(b, e)) / e;
  }
  //减法函数
  subtraction(a, b) {
    var c, d, e;
    try {
        c = a.toString().split(".")[1].length;
    } catch (f) {
        c = 0;
    }
    try {
        d = b.toString().split(".")[1].length;
    } catch (f) {
        d = 0;
    }
    return e = Math.pow(10, Math.max(c, d)), (multiplication(a, e) - multiplication(b, e)) / e;
  }
  //乘法函数
  multiplication(a, b) {
    if(!(a * 1) || !(b * 1)){
      return 0;
    }
    var c = 0,
          d = a.toString(),
          e = b.toString();
  
      try {
          c += d.split(".")[1].length;
      } catch (f) {}
      try {
          c += e.split(".")[1].length;
      } catch (f) {}
      return Number(d.replace(".", "")) * Number(e.replace(".", "")) / Math.pow(10, c);
  }
  //除法函数
  division(a, b) {
    if(!(a * 1) || !(b * 1)){
      return 0;
    }
    var c, d, e = 0,
      f = 0;
    try {
      e = a.toString().split(".")[1].length;
    } catch (g) {}
    try {
      f = b.toString().split(".")[1].length;
    } catch (g) {}
    return c = Number(a.toString().replace(".", "")), d = Number(b.toString().replace(".", "")), multiplication(c / d, Math.pow(10, f - e));
  }
  //深度克隆
  cloneDeep(obj) {
    var objClone = Array.isArray(obj) ? [] : {};
    if (obj && typeof obj === "object") {
      for (let key in obj) {
        if (obj.hasOwnProperty(key)) {
          if (obj[key] && typeof obj[key] === "object"){
              objClone[key] = cloneDeep(obj[key]);
          } else {
              objClone[key] = obj[key];
          }
        }
      }
    }
    return objClone;
  }
  // 颜色转换 (obj)rgba
  parseHexColor(color){
    let hex = color.slice(1);
    let a = 1;
    if (hex.length === 3) {
      hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`;
    }
    if (hex.length === 8) {
      a = parseInt(hex.slice(6), 16) / 255;
      hex = hex.slice(0, 6);
    }
    const bigint = parseInt(hex, 16);
  
    return {
        r: (bigint >> 16) & 255,
        g: (bigint >> 8) & 255,
        b: bigint & 255,
        a,
    } ;
  };
  //颜色转换 (String) rgba
  hextoRgbaString(color,ap){
    let {
      r,g,b,a
    } = this.parseHexColor(color);
    return `rgba(${r},${g},${b},${ap || a})`
  }
  //节流函数
  throttle(func, delay) {
    let timer = null;
    return function () {
      let context = this;
      let args = arguments;
      if (!timer) {
        timer = setTimeout(function () {
          func.apply(context, args);
          timer = null;
        }, delay);
      }
    }
  }
  //防抖函数
  debounce(func, wait, immediate) {
    let timeout, args, context, timestamp, result
  
    const later = function() {
     // 据上一次触发时间间隔
      const last = +new Date() - timestamp
  
     // 上次被包装函数被调用时间间隔last小于设定时间间隔wait
      if (last < wait && last > 0) {
        timeout = setTimeout(later, wait - last)
      } else {
        timeout = null
       // 如果设定为immediate===true，因为开始边界已经调用过了此处无需调用
        if (!immediate) {
          result = func.apply(context, args)
          if (!timeout) context = args = null
        }
      }
    }
  
    return function(...args) {
      context = this
      timestamp = +new Date()
      const callNow = immediate && !timeout
     // 如果延时不存在，重新设定延时
      if (!timeout) timeout = setTimeout(later, wait)
      if (callNow) {
        result = func.apply(context, args)
        context = args = null
      }
  
      return result
    }
  }
  //正则校验
  checkValueByRegExp(value,regStr){
    if(value == undefined || value == null){
      return value;
    }
  
    value = value.toString();
  
    let reg = new RegExp(regStr,'g');
    let arr = value.match(reg);
    let val = arr ? arr.join('') : '';
  
    return val == value;
  }
  //复制文本
  copyToClipboard(text) {
    const input = document.createElement("input");
    document.body.appendChild(input);
    input.style.position = 'absolute';
    input.style.left = '-10000px';
    input.style.top = '-10000px';
    input.value = text;
    input.select();
    document.execCommand("copy");
    document.body.removeChild(input);
  }
  //提取html中的文本
  extractTextFromHTML(html) {
    if(!html){
      return html;
    }
  
    html = html.toString();
    html = html.replace(/<[^>]*>?/gm, ''); // 移除HTML标签
    html = html.replace(/&nbsp;/g, ' '); // 将&nbsp;替换为空格
    return html;
  }
  //遍历所有子节点
  RecursionArray(array,key,callback,parent = null,index = 1){
    if(array){
      for (const item of array) {
        callback(item,parent,index);
        if(item[key] && item[key].length){
          this.RecursionArray(item[key],key,callback,item,index + 1);
        }
      }
    }
  }


}
